package com.zlyx.easy.http.dispatch;

import java.lang.reflect.Method;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zlyx.easy.core.factory.defaults.annotations.FactoryBean;
import com.zlyx.easy.core.factory.interfaces.FactoryBeanDefiner;
import com.zlyx.easy.core.map.ObjectMap;
import com.zlyx.easy.core.utils.LogUtils;
import com.zlyx.easy.http.annotations.HttpService;
import com.zlyx.easy.http.exceptions.MappingHandlerException;
import com.zlyx.easy.http.handlers.AbstractMappingHandler;
import com.zlyx.easy.http.models.RequestModel;
import com.zlyx.easy.http.parser.MappingParser;

/**
 * 请求路由
 * 
 * @Auth 赵光
 * @Describle
 * @2019年12月25日
 */
@FactoryBean(annotationClass = HttpService.class, todo = "请求转发")
public class HttpMappingDispatcher implements FactoryBeanDefiner {

	@Autowired(required = false)
	private MappingParser mappingParser;

	@Override
	public Object excute(Class<?> proxyClass, Method method, ObjectMap params) {
		try {
			HttpService httpService = proxyClass.getAnnotation(HttpService.class);
			String ip = null;
			int port = 8080;
			if (httpService != null) {
				ip = httpService.ip();
				port = httpService.port();
			}
			RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
			PostMapping postMapping = method.getAnnotation(PostMapping.class);
			GetMapping getMapping = method.getAnnotation(GetMapping.class);
			String[] urls = null;
			String beanName = null;
			if (requestMapping != null) {
				urls = requestMapping.value();
				RequestMethod[] httpMethods = requestMapping.method();
				if (httpMethods != null && httpMethods.length > 1) {
					beanName = httpMethods[0].name();
				} else {
					beanName = HttpMethod.POST.name();
				}
			} else if (getMapping != null) {
				urls = getMapping.value();
				beanName = HttpMethod.GET.name();
			} else if (postMapping != null) {
				urls = postMapping.value();
				beanName = HttpMethod.POST.name();
			}
			RequestModel rm = new RequestModel();
			rm.setId(UUID.randomUUID().toString());
			rm.setCls(proxyClass);
			rm.setMethod(beanName);
			rm.setUrls(urls);
			rm.setParams(params);
			rm.setReturnType(method.getReturnType());
			rm.setIp(ip);
			rm.setPort(port);
			if (mappingParser != null) {
				rm = mappingParser.parse(urls, beanName);
			}
			if (rm.getUrls() == null) {
				throw new MappingHandlerException("请求地址不能为空!");
			}
			if (rm.getMethod() == null) {
				throw new MappingHandlerException("请求方式不能为空!");
			}
			return AbstractMappingHandler.call(rm);
		} catch (Exception e) {
			LogUtils.err(e);
		}
		return null;
	}

}
